
import java.util.*;

public class demo1 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null)return list;
        queue.add(root);
        while (!queue.isEmpty()){
            int size=queue.size();
            List<Integer> list1=new ArrayList<>();
            int temp=Integer.MIN_VALUE;
            for (int i = 0; i <size ; i++) {
                TreeNode treeNode=queue.poll();
                temp=Math.max(temp,treeNode.val);
                if(treeNode.left!=null)queue.add(treeNode.left);
                if(treeNode.right!=null)queue.add(treeNode.right);
            }
            list.add(temp);
        }
        return list;
    }
      public class TreeNode {
        int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
